var http = require("http");

var needle = require("needle");
var cheerio = require("cheerio");
var  fs =require("fs");


var imooc = "http://www.imooc.com/learn/348"  //utf-8
var fang = "http://newhouse.nn.fang.com/house/s/b91/";    //gbk2312
var fangindex = "http://www.fang.com";    //gbk2312
var QQ = 'http://www.qq.com/';  //gbk2312

http.createServer(function(req,response){
    var options = {
        compressed         : true, // sets 'Accept-Encoding' to 'gzip,deflate'
        follow_max         : 5,    // follow up to five redirects
        rejectUnauthorized : true,  // verify SSL certificate
        headers:{
            'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding':'gzip, deflate, sdch',
            'Accept-Language':'zh-CN,zh;q=0.8',
            'Cache-Control':'max-age=0',
            'Connection':'keep-alive',
            'Cookie':'imooc_uuid=0cbb84e2-d50a-473a-8ab7-aee0a35ef7e3; imooc_isnew_ct=1471501057; loginstate=1; apsid=ZmMWRhYjNhNjZiZDM3Y2U0Zjc1YTQxNmFmYzBjN2QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMzg1MTMzMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAyNDg4NTIyMzQ0QHFxLmNvbQAAAAAAAAAAAAAAAAAAADc2ZTFlYThiNGExN2MzOGU2ZTVlMmM4NzM5NTJkZWRhdcy2V3XMtlc%3DYW; last_login_username=2488522344%40qq.com; PHPSESSID=dr3dk7tv1o8ctn38b1vhaf9hr7; imooc_isnew=2; cvde=57bbc52ba13ae-3; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1471501055,1471584513,1471586171,1471923496; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1471923525; IMCDNS=0',
            'Host':'www.imooc.com',
            'Upgrade-Insecure-Requests':'1',
            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
        }
    }
    http.get(imooc,function(res) {
        
        console.log(res);
        res.on('data',function(data){

        })
        response.writeHead(200, {'Content-Type': 'text/plain'});
        response.end("res"); 
        
    }).on('error', function(e) {
        console.log("错误：" + e.message);
});;
    

}).listen(5252)
console.log("服务启动端口5252")



// var options = {
//     hostname:'www.imooc.com',
//     port: 80,
//     path: '/learn/348',
//     method: 'GET',
//     headers:{
//         'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
//         'Accept-Encoding':'gzip, deflate, sdch',
//         'Accept-Language':'zh-CN,zh;q=0.8',
//         'Cache-Control':'max-age=0',
//         'Connection':'keep-alive',
//         'Cookie':'imooc_uuid=0cbb84e2-d50a-473a-8ab7-aee0a35ef7e3; imooc_isnew_ct=1471501057; loginstate=1; apsid=ZmMWRhYjNhNjZiZDM3Y2U0Zjc1YTQxNmFmYzBjN2QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMzg1MTMzMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAyNDg4NTIyMzQ0QHFxLmNvbQAAAAAAAAAAAAAAAAAAADc2ZTFlYThiNGExN2MzOGU2ZTVlMmM4NzM5NTJkZWRhdcy2V3XMtlc%3DYW; last_login_username=2488522344%40qq.com; PHPSESSID=dr3dk7tv1o8ctn38b1vhaf9hr7; imooc_isnew=2; cvde=57bbc52ba13ae-3; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1471501055,1471584513,1471586171,1471923496; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1471923525; IMCDNS=0',
//         'Host':'www.imooc.com',
//         'Upgrade-Insecure-Requests':'1',
//         'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
//     }
// }








// var fs     = require('fs'),
//     needle = require('needle'),
//     path   = require('path');

// var url    = process.argv[2] || '';
// var file   = path.basename(url);

// console.log('Downloading ' + file);
// needle.get(url).pipe(fs.createWriteStream(file));
